package org.dding.mvc;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

/**
 * 调用上下文，封装调用过程中的所有资源
 */
public class InvocationContext {

	public static final String RESPONSE_CONTENT = "response_content";
	public static final String RESPONSE_HEADER = "response_header";

	private String actionName;

	/**
	 * ActionMethod名称
	 */
	private String actionMethodName;

	/**
	 * 是否是http post
	 */
	private boolean isPost = false;

	/**
	 * 执行的Action方法
	 */
	private Method actionMethod = null;

	/**
	 * 表单 和 QueryString 提交的数据
	 */
	private Map<String, String[]> parameterMap = new HashMap<String, String[]>();

	/**
	 * 上传的文件
	 */
	private Map<String, FileUploaded> fileUploadedMap = new HashMap<String, FileUploaded>();

	private PageContext pageContext;
	private SessionContext sessionContext;
	private Invocation invocation;

	private ServletConfig servletConfig;
	private String serviceName;

	private Map<String, Object> response = new HashMap<String, Object>();

	private HttpServletRequest request = null;

	public InvocationContext(ServletConfig servletConfig,
			HttpServletRequest request, Map<String, String[]> parameterMap,
			Map<String, FileUploaded> fileUploadedMap, String actionName,
			String actionMethodName, boolean isPostMethod) {
		this.servletConfig = servletConfig;
		this.request = request;
		this.parameterMap.putAll(parameterMap);
		this.fileUploadedMap.putAll(fileUploadedMap);
		this.actionName = actionName;
		this.actionMethodName = actionMethodName;
		this.isPost = isPostMethod;
		this.sessionContext = SessionContext.currentThreadSessionContext();
		this.pageContext = new PageContext();
	}

	HttpServletRequest getServletRequest() {
		return request;
	}

	public void addResponse(String key, Object value) {
		response.put(key, value);
	}

	public Object getResponse(String key) {
		return response.get(key);
	}

	public String getRequestURI() {
		return getServletRequest().getRequestURI();
	}

	public ServletConfig getServletConfig() {
		return servletConfig;
	}

	public ServletContext getServletContext() {
		return servletConfig.getServletContext();
	}

	void setActionMethod(Method actionMethod) {
		this.actionMethod = actionMethod;
	}

	public Method getActionMethod() {
		return actionMethod;
	}

	public String getActionMethodName() {
		return actionMethodName;
	}

	public String getFullActionMethodName() {
		return getActionName() + "." + getActionMethodName();
	}

	public String getActionName() {
		return actionName;
	}

	public boolean isPost() {
		return isPost;
	}

	public SessionContext getSessionContext() {
		return sessionContext;
	}

	void setInvocation(Invocation invocation) {
		this.invocation = invocation;
	}

	public Invocation getInvocation() {
		return invocation;
	}

	public PageContext getPageContext() {
		return pageContext;
	}

	public String getServiceName() {
		return serviceName;
	}

	void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getParameterValue(String key) {
		String[] values = parameterMap.get(key);
		if (values != null)
			return values[0];
		return null;
	}

	public String[] getParameterValues(String key) {
		return parameterMap.get(key);
	}

	public Map<String, String[]> getParameterMap() {
		return Collections.unmodifiableMap(parameterMap);
	}

	public Map<String, FileUploaded> getFileUploadedMap() {
		return fileUploadedMap;
	}

	public FileUploaded getFileUploaded(String key) {
		return fileUploadedMap.get(key);
	}

	public String getWebPath() {
		return servletConfig.getServletContext().getRealPath("/");
	}
}
